package cn.lisens.demo.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import cn.lisens.demo.model.Customer;
import cn.lisens.demo.model.TestWebRequest;
import cn.lisens.demo.service.TestWebClientService;

@RestController
public class TestWebClientController {

	@Autowired
	TestWebClientService testWebClientService;
	
	@RequestMapping("login")
	@ResponseBody
	public String login(TestWebRequest webRequest) {
		return testWebClientService.login(webRequest);
	}
	
	@RequestMapping("listData")
	@ResponseBody
	public List<Customer> listData(TestWebRequest webRequest) {
		return testWebClientService.listData(webRequest);
	}
	
	@RequestMapping("async")
	@ResponseBody
	public ResponseEntity<String> async() {
		testWebClientService.async();
		return ResponseEntity.ok().build();
	}
	
	@RequestMapping("batch")
	@ResponseBody
	public ResponseEntity<String> batch() {
		testWebClientService.batchSave();
		return ResponseEntity.ok().build();
	}
	
	@ExceptionHandler(WebClientResponseException.class)
	public String handleWebClientResponseException(WebClientResponseException ex) {
		return ex.getMessage();
	}
}
